home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / IconView.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  169 lines

  1. /*
  2.  * @(#)IconView.java    1.15 98/04/09
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.text;
  21.  
  22. import java.awt.*;
  23. import com.sun.java.swing.Icon;
  24. import com.sun.java.swing.event.*;
  25.  
  26. /**
  27.  * Icon decorator that implements the view interface.  The 
  28.  * entire element is used to represent the icon.  This acts
  29.  * as a gateway from the display-only View implementations to
  30.  * interactive lightweight icons (that is, it allows icons
  31.  * to be embedded into the View hierarchy.  The parent of the icon
  32.  * is the container that is handed out by the associated view 
  33.  * factory.
  34.  *
  35.  * @author Timothy Prinzing
  36.  * @version 1.15 04/09/98
  37.  */
  38. public class IconView extends View  {
  39.  
  40.     /**
  41.      * Creates a new icon view that represents an element.
  42.      *
  43.      * @param elem the element to create a view for
  44.      */
  45.     public IconView(Element elem) {
  46.     super(elem);
  47.     AttributeSet attr = elem.getAttributes();
  48.     c = StyleConstants.getIcon(attr);
  49.     }
  50.  
  51.     // --- View methods ---------------------------------------------
  52.  
  53.     /**
  54.      * Paints the icon.
  55.      * The real paint behavior occurs naturally from the association
  56.      * that the icon has with its parent container (the same
  57.      * container hosting this view), so this simply allows us to 
  58.      * position the icon properly relative to the view.  Since
  59.      * the coordinate system for the view is simply the parent 
  60.      * containers, positioning the child icon is easy.
  61.      *
  62.      * @param g the rendering surface to use
  63.      * @param a the allocated region to render into
  64.      * @see View#paint
  65.      */
  66.     public void paint(Graphics g, Shape a) {
  67.     Rectangle alloc = a.getBounds();
  68.     c.paintIcon(getContainer(), g, alloc.x, alloc.y);
  69.     }
  70.  
  71.     /**
  72.      * Determines the preferred span for this view along an
  73.      * axis.
  74.      *
  75.      * @param axis may be either View.X_AXIS or View.Y_AXIS
  76.      * @returns  the span the view would like to be rendered into.
  77.      *           Typically the view is told to render into the span
  78.      *           that is returned, although there is no guarantee.  
  79.      *           The parent may choose to resize or break the view.
  80.      * @exception IllegalArgumentException for an invalid axis
  81.      */
  82.     public float getPreferredSpan(int axis) {
  83.     switch (axis) {
  84.     case View.X_AXIS:
  85.         return c.getIconWidth();
  86.     case View.Y_AXIS:
  87.         return c.getIconHeight();
  88.     default:
  89.         throw new IllegalArgumentException("Invalid axis: " + axis);
  90.     }
  91.     }
  92.  
  93.     /**
  94.      * Determines the desired alignment for this view along an
  95.      * axis.  This is implemented to give the alignment to the
  96.      * bottom of the icon along the y axis, and the default
  97.      * along the x axis.
  98.      *
  99.      * @param axis may be either View.X_AXIS or View.Y_AXIS
  100.      * @returns the desired alignment >= 0.0f && <= 1.0f.  This should be
  101.      *   a value between 0.0 and 1.0 where 0 indicates alignment at the
  102.      *   origin and 1.0 indicates alignment to the full span
  103.      *   away from the origin.  An alignment of 0.5 would be the
  104.      *   center of the view.
  105.      */
  106.     public float getAlignment(int axis) {
  107.     switch (axis) {
  108.     case View.Y_AXIS:
  109.         return 1;
  110.     default:
  111.         return super.getAlignment(axis);
  112.     }
  113.     }
  114.  
  115.     /**
  116.      * Provides a mapping from the document model coordinate space
  117.      * to the coordinate space of the view mapped to it.
  118.      *
  119.      * @param pos the position to convert >= 0
  120.      * @param a the allocated region to render into
  121.      * @return the bounding box of the given position
  122.      * @exception BadLocationException  if the given position does not
  123.      *   represent a valid location in the associated document
  124.      * @see View#modelToView
  125.      */
  126.     public Shape modelToView(int pos, Shape a) throws BadLocationException {
  127.     int p0 = getStartOffset();
  128.     int p1 = getEndOffset();
  129.     if ((pos >= p0) && (pos < p1)) {
  130.         Rectangle r = new Rectangle(a.getBounds());
  131.         r.width = 0;
  132.         return r;
  133.     }
  134.     return null;
  135.     }
  136.  
  137.     /**
  138.      * Provides a mapping from the view coordinate space to the logical
  139.      * coordinate space of the model.
  140.      *
  141.      * @param x the X coordinate >= 0
  142.      * @param y the Y coordinate >= 0
  143.      * @param a the allocated region to render into
  144.      * @return the location within the model that best represents the
  145.      *  given point of view >= 0
  146.      * @see View#viewToModel
  147.      */
  148.     public int viewToModel(float x, float y, Shape a) {
  149.     Rectangle alloc = a.getBounds();
  150.     return getStartOffset();
  151.     }
  152.  
  153.     /**
  154.      * Sets the size of the view.  Since Icon doesn't
  155.      * support this functionality, there is nothing 
  156.      * we can do.
  157.      *
  158.      * @param width the width
  159.      * @param height the height
  160.      */
  161.     public void setSize(float width, float height) {
  162.     }
  163.  
  164.     // --- member variables ------------------------------------------------
  165.  
  166.     private Icon c;
  167. }
  168.  
  169.